/*----------------------------------------------------------------------------

	Copyright (c) 2006, Creator of the Cant Project.
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	+ Redistributions of source code must retain the above copyright notice,
	this list of conditions and the following disclaimer.

	+ Redistributions in binary form must reproduce the above copyright notice,
	this list of conditions and the following disclaimer in the documentation
	and/or other materials provided with the distribution.

	+ Neither the name of the Creator of the Cant Project nor the names of its
	contributors may be used to endorse or promote products derived from this
	software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.

----------------------------------------------------------------------------*/

#include	"testcase.hpp"



/**
	Constructor.
	
	@param		a_strMessage		reason for exception.
*/
TestCaseException::TestCaseException(const std::string& a_strMessage )	: Exception(a_strMessage )
{}



//--------------------------------------------------------------------------//



/**
	Constructor.
	
	@param		a_strName		name of test case.
*/
TestCase::TestCase(const std::string& a_strName )
{
	m_strName				= a_strName;
}


/**
	Destructor.
	
*/
TestCase::~TestCase() throw()
{}
		
		
/**
	Gives the name of this test case.
	
	@return		test case name.
*/
std::string	TestCase::getName(void ) const
{
	return	m_strName;
}
		
		
/**
	Gives the number of registered tests.
	
	@return		number of registered tests.
*/
int	TestCase::getCount(void ) const
{
	return	m_listCaseMethods.size();
}


/**
	Called before each test case.
	
*/
void	TestCase::setup(void )
{}


/**
	Called after each test case.
	
*/
void	TestCase::teardown(void )
{}


/**
	Adds a method to be called for a test case.
	
	@param		a_method		method to be called.
*/
void	TestCase::addMethod(typeCaseMethod a_method )
{
	m_listCaseMethods.push_back(a_method );
}


/**
	Invokes all registered test cases.
	
	@return		number of failed tests.
*/
int	TestCase::run(void )
{
	int					iFailCount				= 0;
	
	for (typeCaseMethodList::iterator iter = m_listCaseMethods.begin(); iter != m_listCaseMethods.end(); ++iter )
	{
		try
		{
			typeCaseMethod		pfnCaseMethod			= *iter;
			
			//MW:	For each case method call setup, then the method, then teardown...
			setup();
			(*pfnCaseMethod )(this );
			teardown();
		}
		catch(Exception e )
		{
			++iFailCount;
			
			Log::write("error: [" + e.getType() + "] - "  + e.getMessage() );
		}
		catch(... )
		{
			Log::write("unknown exception" );
		}
	}
	
	return	iFailCount;
}
		
		
/**
	Pre-condition assertion.
	
	@param		a_bTest			test condition.
	@param		a_strMessage	message to be output on test failure.
*/
void	TestCase::assertPre(bool a_bTest, const std::string a_strMessage ) const
{
	if (! a_bTest )
	{
		throw TestCaseException("[" + m_strName + "] pre-condition failed  : " + a_strMessage );
	}
}


/**
	Post-condition assertion.
	
	@param		a_bTest			test condition.
	@param		a_strMessage	message to be output on test failure.
*/
void	TestCase::assertPost(bool a_bTest, const std::string a_strMessage ) const
{
	if (! a_bTest )
	{
		throw TestCaseException("[" + m_strName + "] post-condition failed : " + a_strMessage );
	}
}


/**
	Test assertion.
	
	@param		a_bTest			test condition.
	@param		a_strMessage	message to be output on test failure.
*/
void	TestCase::assertTrue(bool a_bTest, const std::string a_strMessage ) const
{
	if (! a_bTest )
	{
		throw TestCaseException("[" + m_strName + "] assertion failed     : " + a_strMessage );
	}
}
